From bdb496688f48b88d08f457b27669fc30250ad52a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 31 Oct 2014 09:37:44 -0700 Subject: [PATCH] Update to rust master --- Cargo.lock | 14 ++-- src/bin/bench.rs | 2 +- src/bin/test.rs | 2 +- src/cargo/core/shell.rs | 93 +++++++++--------------- src/cargo/lib.rs | 6 +- src/cargo/ops/cargo_clean.rs | 2 +- src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/ops/cargo_generate_lockfile.rs | 2 +- src/cargo/ops/cargo_read_manifest.rs | 4 +- src/cargo/ops/cargo_rustc/job_queue.rs | 2 +- src/cargo/ops/cargo_rustc/mod.rs | 2 +- src/cargo/util/config.rs | 2 +- src/cargo/util/dependency_queue.rs | 2 +- src/cargo/util/errors.rs | 83 ++++++--------------- src/cargo/util/result.rs | 2 +- tests/test_shell.rs | 45 +++++++----- 16 files changed, 102 insertions(+), 163 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 23e5cac16..1c3bcdb27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,12 +44,12 @@ source = "git+https://github.com/lifthrasiir/rust-encoding#6daaad9f4c0fcde452582 [[package]] name = "flate2" version = "0.0.1" -source = "git+https://github.com/alexcrichton/flate2-rs#f74a1f632c09e4f241ecb638e331c2cec4e955e0" +source = "git+https://github.com/alexcrichton/flate2-rs#67be37548937c059370ce272efe1142fbf198d67" [[package]] name = "git2" version = "0.0.1" -source = "git+https://github.com/alexcrichton/git2-rs#fc308a305068392b07999b7c47122244b151cdec" +source = "git+https://github.com/alexcrichton/git2-rs#35a0632e92d63de8218675a17cd81e15d0de44b4" dependencies = [ "libgit2 0.0.1 (git+https://github.com/alexcrichton/git2-rs)", "url 0.1.0 (git+https://github.com/servo/rust-url)", @@ -68,7 +68,7 @@ source = "git+https://github.com/carllerche/hamcrest-rust.git#998036d000095f72c8 [[package]] name = "libgit2" version = "0.0.1" -source = "git+https://github.com/alexcrichton/git2-rs#fc308a305068392b07999b7c47122244b151cdec" +source = "git+https://github.com/alexcrichton/git2-rs#35a0632e92d63de8218675a17cd81e15d0de44b4" dependencies = [ "libssh2-static-sys 0.0.1 (git+https://github.com/alexcrichton/libssh2-static-sys)", "link-config 0.0.1 (git+https://github.com/alexcrichton/link-config)", @@ -83,7 +83,7 @@ source = "git+https://github.com/alexcrichton/libssh2-static-sys#80e71a3021618eb [[package]] name = "link-config" version = "0.0.1" -source = "git+https://github.com/alexcrichton/link-config#ed9fffa15fc41e8f09e92c7756feead1b0cbb01d" +source = "git+https://github.com/alexcrichton/link-config#7c56ef12c8a49bedb0357e2a50af9ba79bf0ae1b" [[package]] name = "openssl-static-sys" @@ -100,17 +100,17 @@ dependencies = [ [[package]] name = "semver" version = "0.1.0" -source = "git+https://github.com/rust-lang/semver#fdaec35ff5db609f53119f1e455fdd8830edc790" +source = "git+https://github.com/rust-lang/semver#7dca047a9cd40e929a4545b37a1917daff82f156" [[package]] name = "tar" version = "0.0.1" -source = "git+https://github.com/alexcrichton/tar-rs#fced2e3833ed2776d9975ffc6fb3204528944faa" +source = "git+https://github.com/alexcrichton/tar-rs#47d2cc4b09e373a4cc7bee7c71ebf96b42ea620d" [[package]] name = "toml" version = "0.1.0" -source = "git+https://github.com/alexcrichton/toml-rs#35804927ef76cc5f24b04f2af25919c488a0992c" +source = "git+https://github.com/alexcrichton/toml-rs#71d1689d63ef1af41addef330e314a56e88f48a9" [[package]] name = "url" diff --git a/src/bin/bench.rs b/src/bin/bench.rs index 56e441cfa..612c37b15 100644 --- a/src/bin/bench.rs +++ b/src/bin/bench.rs @@ -75,7 +75,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult Some(err) => { Err(match err.exit { Some(ExitStatus(i)) => CliError::new("", i as uint), - _ => CliError::from_boxed(err.mark_human(), 101) + _ => CliError::from_boxed(err.concrete().mark_human(), 101) }) } } diff --git a/src/bin/test.rs b/src/bin/test.rs index 0dd4d4c30..b1261a6dd 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -74,7 +74,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult Some(err) => { Err(match err.exit { Some(ExitStatus(i)) => CliError::new("", i as uint), - _ => CliError::from_boxed(err.mark_human(), 101) + _ => CliError::from_boxed(err.concrete().mark_human(), 101) }) } } diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index b5570f0dd..3ee6a916f 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -1,4 +1,4 @@ -use term::{mod, Terminal, color}; +use term::{Terminal, TerminfoTerminal, color}; use term::color::{Color, BLACK, RED, GREEN, YELLOW}; use term::attr::{Attr, Bold}; use std::io::{IoResult, stderr}; @@ -10,34 +10,38 @@ pub struct ShellConfig { pub tty: bool } -enum AdequateTerminal<'a> { - NoColor(Box), - Colored(Box>+'a>) +enum AdequateTerminal { + NoColor(Box), + Colored(Box + Send>) } -pub struct Shell<'a> { - terminal: AdequateTerminal<'a>, - config: ShellConfig +pub struct Shell { + terminal: AdequateTerminal, + config: ShellConfig, } -pub struct MultiShell<'a> { - out: Shell<'a>, - err: Shell<'a>, +pub struct MultiShell { + out: Shell, + err: Shell, verbose: bool } pub type Callback<'a> = |&mut MultiShell|:'a -> IoResult<()>; -impl<'a> MultiShell<'a> { - pub fn new(out: Shell<'a>, err: Shell<'a>, verbose: bool) -> MultiShell<'a> { +struct UghWhyIsThisNecessary { + inner: Box, +} + +impl MultiShell { + pub fn new(out: Shell, err: Shell, verbose: bool) -> MultiShell { MultiShell { out: out, err: err, verbose: verbose } } - pub fn out(&mut self) -> &mut Shell<'a> { + pub fn out(&mut self) -> &mut Shell { &mut self.out } - pub fn err(&mut self) -> &mut Shell<'a> { + pub fn err(&mut self) -> &mut Shell { &mut self.err } @@ -72,20 +76,21 @@ impl<'a> MultiShell<'a> { } } -pub type ShellCallback<'a> = |&mut Shell<'a>|:'a -> IoResult<()>; +pub type ShellCallback<'a> = |&mut Shell|:'a -> IoResult<()>; -impl<'a> Shell<'a> { - pub fn create(out: Box, config: ShellConfig) -> Shell<'a> { +impl Shell { + pub fn create(out: Box, config: ShellConfig) -> Shell { + let out = UghWhyIsThisNecessary { inner: out }; if config.tty && config.color { - let term: Option>> = Terminal::new(out); + let term = TerminfoTerminal::new(out); term.map(|t| Shell { - terminal: Colored(box t as Box>>), + terminal: Colored(t), config: config }).unwrap_or_else(|| { - Shell { terminal: NoColor(box stderr() as Box), config: config } + Shell { terminal: NoColor(box stderr()), config: config } }) } else { - Shell { terminal: NoColor(out), config: config } + Shell { terminal: NoColor(out.inner), config: config } } } @@ -119,19 +124,6 @@ impl<'a> Shell<'a> { try!(self.flush()); Ok(()) } -} - -impl<'a> Terminal> for Shell<'a> { - fn new(out: Box) -> Option> { - Some(Shell { - terminal: NoColor(out), - config: ShellConfig { - color: true, - verbose: false, - tty: false, - } - }) - } fn fg(&mut self, color: color::Color) -> IoResult { match self.terminal { @@ -140,13 +132,6 @@ impl<'a> Terminal> for Shell<'a> { } } - fn bg(&mut self, color: color::Color) -> IoResult { - match self.terminal { - Colored(ref mut c) => c.bg(color), - NoColor(_) => Ok(false) - } - } - fn attr(&mut self, attr: Attr) -> IoResult { match self.terminal { Colored(ref mut c) => c.attr(attr), @@ -167,27 +152,9 @@ impl<'a> Terminal> for Shell<'a> { NoColor(_) => Ok(()) } } - - fn unwrap(self) -> Box { - panic!("Can't unwrap a Shell"); - } - - fn get_ref<'b>(&'b self) -> &'b Box { - match self.terminal { - Colored(ref c) => c.get_ref(), - NoColor(ref w) => w - } - } - - fn get_mut<'b>(&'b mut self) -> &'b mut Box { - match self.terminal { - Colored(ref mut c) => c.get_mut(), - NoColor(ref mut w) => w - } - } } -impl<'a> Writer for Shell<'a> { +impl Writer for Shell { fn write(&mut self, buf: &[u8]) -> IoResult<()> { match self.terminal { Colored(ref mut c) => c.write(buf), @@ -202,3 +169,9 @@ impl<'a> Writer for Shell<'a> { } } } + +impl Writer for UghWhyIsThisNecessary { + fn write(&mut self, bytes: &[u8]) -> IoResult<()> { + self.inner.write(bytes) + } +} diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 17a84c7b3..800f2d59d 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -150,15 +150,15 @@ pub fn process_executed<'a, } } -pub fn shell(verbose: bool) -> MultiShell<'static> { +pub fn shell(verbose: bool) -> MultiShell { let tty = stderr_raw().isatty(); - let stderr = box stderr() as Box; + let stderr = box stderr() as Box; let config = ShellConfig { color: true, verbose: verbose, tty: tty }; let err = Shell::create(stderr, config); let tty = stdout_raw().isatty(); - let stdout = box stdout() as Box; + let stdout = box stdout() as Box; let config = ShellConfig { color: true, verbose: verbose, tty: tty }; let out = Shell::create(stdout, config); diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 881ebf342..0560225a3 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -9,7 +9,7 @@ use ops::{mod, Layout, Context}; pub struct CleanOptions<'a> { pub spec: Option<&'a str>, pub target: Option<&'a str>, - pub shell: &'a mut MultiShell<'a> + pub shell: &'a mut MultiShell, } /// Cleans the project from build artifacts. diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 8e44a81d8..18ed9328d 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -36,7 +36,7 @@ use util::{CargoResult, Wrap, config, internal, human, ChainError, profile}; /// Contains informations about how a package should be compiled. pub struct CompileOptions<'a> { pub env: &'a str, - pub shell: &'a mut MultiShell<'a>, + pub shell: &'a mut MultiShell, /// Number of concurrent jobs to use. pub jobs: Option, /// The target platform to compile for (example: `i686-unknown-linux-gnu`). diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index 059e9064c..19cfdee2a 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -9,7 +9,7 @@ use util::config::{Config}; use util::{CargoResult, human}; pub struct UpdateOptions<'a> { - pub shell: &'a mut MultiShell<'a>, + pub shell: &'a mut MultiShell, pub to_update: Option<&'a str>, pub precise: Option<&'a str>, pub aggressive: bool, diff --git a/src/cargo/ops/cargo_read_manifest.rs b/src/cargo/ops/cargo_read_manifest.rs index bdaa89b8b..955c15bcc 100644 --- a/src/cargo/ops/cargo_read_manifest.rs +++ b/src/cargo/ops/cargo_read_manifest.rs @@ -3,7 +3,7 @@ use std::io::{mod, File, fs}; use std::io::fs::PathExtensions; use core::{Package,Manifest,SourceId}; -use util::{mod, CargoResult, human, CargoError}; +use util::{mod, CargoResult, human, FromError}; use util::important_paths::find_project_manifest_exact; use util::toml::{Layout, project_layout}; @@ -66,7 +66,7 @@ fn walk(path: &Path, is_root: bool, let dirs = match fs::readdir(path) { Ok(dirs) => dirs, Err(ref e) if e.kind == io::PermissionDenied => return Ok(()), - Err(e) => return Err(e.box_error()), + Err(e) => return Err(FromError::from_error(e)), }; for dir in dirs.iter() { try!(walk(dir, false, |a, x| callback(a, x))) diff --git a/src/cargo/ops/cargo_rustc/job_queue.rs b/src/cargo/ops/cargo_rustc/job_queue.rs index 14df4eeaa..9c1b5f7b8 100644 --- a/src/cargo/ops/cargo_rustc/job_queue.rs +++ b/src/cargo/ops/cargo_rustc/job_queue.rs @@ -124,7 +124,7 @@ impl<'a, 'b> JobQueue<'a, 'b> { self.active -= 1; match result { Ok(()) => { - let state = self.pending.get_mut(&(id, stage)); + let state = &mut self.pending[(id, stage)]; state.amt -= 1; state.fresh = state.fresh.combine(fresh); if state.amt == 0 { diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 05e3c0d0a..ee023be19 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -277,7 +277,7 @@ fn compile_custom(pkg: &Package, cmd: &str, try!(p.exec_with_output().map(|_| ()).map_err(|mut e| { e.msg = format!("Failed to run custom build command for `{}`\n{}", pkg, e.msg); - e.mark_human() + e.concrete().mark_human() })); Ok(()) }) diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 20c37f3c9..bbb857562 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -14,7 +14,7 @@ use util::toml as cargo_toml; pub struct Config<'a> { home_path: Path, - shell: &'a mut MultiShell<'a>, + shell: &'a mut MultiShell, jobs: uint, target: Option, linker: Option, diff --git a/src/cargo/util/dependency_queue.rs b/src/cargo/util/dependency_queue.rs index a07c598ef..1807ed284 100644 --- a/src/cargo/util/dependency_queue.rs +++ b/src/cargo/util/dependency_queue.rs @@ -125,7 +125,7 @@ impl, V> DependencyQueue { if fresh == Dirty { self.dirty.insert(dep.clone()); } - assert!(self.dep_map.get_mut(dep).mut0().remove(key)); + assert!(self.dep_map[*dep].mut0().remove(key)); } } } diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index 3653a491d..0c6132514 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -17,14 +17,6 @@ pub trait CargoError: Send { fn cause(&self) -> Option<&CargoError> { None } fn is_human(&self) -> bool { false } - fn to_error>(self) -> E { - FromError::from_error(self) - } - - fn box_error(self) -> Box { - box self as Box - } - fn concrete(&self) -> ConcreteCargoError { ConcreteCargoError { description: self.description(), @@ -33,18 +25,6 @@ pub trait CargoError: Send { is_human: self.is_human() } } - - fn with_cause(self, cause: E) -> Box { - let mut concrete = self.concrete(); - concrete.cause = Some(cause.box_error()); - box concrete as Box - } - - fn mark_human(self) -> Box { - let mut concrete = self.concrete(); - concrete.is_human = true; - box concrete as Box - } } pub trait FromError { @@ -53,7 +33,7 @@ pub trait FromError { impl FromError for Box { fn from_error(error: E) -> Box { - error.box_error() + box error as Box } } @@ -75,25 +55,10 @@ impl Show for Box { } impl CargoError for Box { - fn description(&self) -> String { - (**self).description() - } - - fn detail(&self) -> Option { - (**self).detail() - } - - fn cause(&self) -> Option<&CargoError> { - (**self).cause() - } - - fn is_human(&self) -> bool { - (**self).is_human() - } - - fn box_error(self) -> Box { - self - } + fn description(&self) -> String { (**self).description() } + fn detail(&self) -> Option { (**self).detail() } + fn cause(&self) -> Option<&CargoError> { (**self).cause() } + fn is_human(&self) -> bool { (**self).is_human() } } impl CargoError for semver::ReqParseError { @@ -114,19 +79,19 @@ pub trait ChainError { impl<'a, T> ChainError for ||:'a -> CargoResult { fn chain_error(self, callback: || -> E) -> CargoResult { - self().map_err(|err| callback().with_cause(err)) + self().map_err(|err| callback().concrete().with_cause(err)) } } impl BoxError for Result { fn box_error(self) -> CargoResult { - self.map_err(|err| err.box_error()) + self.map_err(|err| box err as Box) } } impl ChainError for Result { fn chain_error(self, callback: || -> E) -> CargoResult { - self.map_err(|err| callback().with_cause(err)) + self.map_err(|err| callback().concrete().with_cause(err)) } } @@ -222,12 +187,6 @@ impl CargoError for ProcessError { fn cause(&self) -> Option<&CargoError> { self.cause.as_ref().map(|c| { let err: &CargoError = &**c; err }) } - - fn with_cause(mut self, - err: E) -> Box { - self.cause = Some(err.box_error()); - box self as Box - } } pub struct ConcreteCargoError { @@ -237,6 +196,19 @@ pub struct ConcreteCargoError { is_human: bool } +impl ConcreteCargoError { + pub fn with_cause(mut self, + err: E) -> Box { + self.cause = Some(box err as Box); + box self as Box + } + + pub fn mark_human(mut self) -> Box { + self.is_human = true; + box self as Box + } +} + impl Show for ConcreteCargoError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}", self.description) @@ -256,17 +228,6 @@ impl CargoError for ConcreteCargoError { self.cause.as_ref().map(|c| { let err: &CargoError = &**c; err }) } - fn with_cause(mut self, - err: E) -> Box { - self.cause = Some(err.box_error()); - box self as Box - } - - fn mark_human(mut self) -> Box { - self.is_human = true; - box self as Box - } - fn is_human(&self) -> bool { self.is_human } @@ -385,7 +346,7 @@ pub fn caused_human(error: S, cause: E) -> Box), is_human: true } as Box } diff --git a/src/cargo/util/result.rs b/src/cargo/util/result.rs index 108e08488..e415f7288 100644 --- a/src/cargo/util/result.rs +++ b/src/cargo/util/result.rs @@ -8,7 +8,7 @@ impl Wrap for Result> { fn wrap(self, error: E) -> CargoResult { match self { Ok(x) => Ok(x), - Err(e) => Err(error.with_cause(e)) + Err(e) => Err(error.concrete().with_cause(e)) } } } diff --git a/tests/test_shell.rs b/tests/test_shell.rs index d1211ab2b..a500d0827 100644 --- a/tests/test_shell.rs +++ b/tests/test_shell.rs @@ -1,55 +1,60 @@ -use support::{ResultTest,Tap,shell_writes}; +use std::io::{MemWriter, IoResult, ChanReader, ChanWriter}; +use term::{Terminal, TerminfoTerminal, color}; use hamcrest::{assert_that}; -use std::io::{MemWriter, BufWriter, IoResult}; + use cargo::core::shell::{Shell,ShellConfig}; -use term::{Terminal,TerminfoTerminal,color}; + +use support::{ResultTest,Tap,shell_writes}; fn setup() { } -fn writer(buf: &mut [u8]) -> Box { - box BufWriter::new(buf) as Box +fn pair() -> (ChanWriter, ChanReader) { + let (tx, rx) = channel(); + (ChanWriter::new(tx), ChanReader::new(rx)) } test!(non_tty { let config = ShellConfig { color: true, verbose: true, tty: false }; - let mut buf: Vec = Vec::from_elem(9, 0 as u8); + let (tx, mut rx) = pair(); - Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| { + Shell::create(box tx, config).tap(|shell| { shell.say("Hey Alex", color::RED).assert(); - assert_that(buf.as_slice(), shell_writes("Hey Alex\n")); }); + + let buf = rx.read_to_end().unwrap(); + assert_that(buf.as_slice(), shell_writes("Hey Alex\n")); }) test!(color_explicitly_disabled { let config = ShellConfig { color: false, verbose: true, tty: true }; - let mut buf: Vec = Vec::from_elem(9, 0 as u8); + let (tx, mut rx) = pair(); - Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| { + Shell::create(box tx, config).tap(|shell| { shell.say("Hey Alex", color::RED).assert(); - assert_that(buf.as_slice(), shell_writes("Hey Alex\n")); }); + let buf = rx.read_to_end().unwrap(); + assert_that(buf.as_slice(), shell_writes("Hey Alex\n")); }) test!(colored_shell { - let term: Option> = - Terminal::new(MemWriter::new()); + let term = TerminfoTerminal::new(MemWriter::new()); if term.is_none() { return } let config = ShellConfig { color: true, verbose: true, tty: true }; - let mut buf: Vec = Vec::from_elem(100, 0 as u8); + let (tx, mut rx) = pair(); - Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| { + Shell::create(box tx, config).tap(|shell| { shell.say("Hey Alex", color::RED).assert(); - let buf = buf.as_slice().slice_to(buf.iter().position(|a| *a == 0).unwrap()); - assert_that(buf, shell_writes(colored_output("Hey Alex\n", - color::RED).assert())); }); + let buf = rx.read_to_end().unwrap(); + assert_that(buf.as_slice(), + shell_writes(colored_output("Hey Alex\n", + color::RED).assert())); }) fn colored_output(string: S, color: color::Color) -> IoResult { - let mut term: TerminfoTerminal = - Terminal::new(MemWriter::new()).assert(); + let mut term = TerminfoTerminal::new(MemWriter::new()).unwrap(); try!(term.reset()); try!(term.fg(color)); try!(term.write_str(string.as_slice())); -- 2.30.2